package com.next

import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*
import org.springframework.security.access.annotation.Secured
@Secured(['ROLE_ADMINISTRATOR', 'ROLE_CUSTOMER_SERVICE_REPRESENTATIVE', 'ROLE_ACCOUNT_MANAGER', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_GENERAL_RISK_MANAGER', 'ROLE_BRANCH_GENERAL_MANAGER', 'ROLE_BRANCH_OFFICE_RISK_MANAGER', 'ROLE_HEAD_OFFICE_RISK_MANAGER', 'ROLE_GENERAL_MANAGER', 'ROLE_CRO', 'ROLE_CEO', 'ROLE_POST_LOAN_MANAGER', 'ROLE_BRANCH_OFFICE_POST_LOAN_MANAGER'])
class VehicleMarqueController {

    VehicleMarqueService vehicleMarqueService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond vehicleMarqueService.list(params), model:[vehicleMarqueCount: vehicleMarqueService.count()]
    }

    def show(Long id) {
        respond vehicleMarqueService.get(id)
    }

    def create() {
        respond new VehicleMarque(params)
    }

    def save(VehicleMarque vehicleMarque) {
        if (vehicleMarque == null) {
            notFound()
            return
        }

        try {
            vehicleMarqueService.save(vehicleMarque)
        } catch (ValidationException e) {
            respond vehicleMarque.errors, view:'create'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'vehicleMarque.label', default: 'VehicleMarque'), vehicleMarque.id])
                // redirect vehicleMarque
                redirect(controller: "vehicleSeries", action: "show", params: [id: vehicleMarque?.series?.id])
            }
            '*' { respond vehicleMarque, [status: CREATED] }
        }
    }

    def edit(Long id) {
        respond vehicleMarqueService.get(id)
    }

    def update(VehicleMarque vehicleMarque) {
        if (vehicleMarque == null) {
            notFound()
            return
        }

        try {
            vehicleMarqueService.save(vehicleMarque)
        } catch (ValidationException e) {
            respond vehicleMarque.errors, view:'edit'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'vehicleMarque.label', default: 'VehicleMarque'), vehicleMarque.id])
                // redirect vehicleMarque
                redirect(controller: "vehicleSeries", action: "show", params: [id: vehicleMarque?.series?.id])
            }
            '*'{ respond vehicleMarque, [status: OK] }
        }
    }

    def delete(Long id) {
        if (id == null) {
            notFound()
            return
        }

        vehicleMarqueService.delete(id)

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'vehicleMarque.label', default: 'VehicleMarque'), id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicleMarque.label', default: 'VehicleMarque'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
